<?php

namespace app\admin\controller\classroom;

use app\common\controller\Backend;
use fast\Tree;
use think\Db;

/**
 * 课程分类
 *
 * @icon fa fa-circle-o
 */
class Type extends Backend
{

    /**
     * Type模型对象
     * @var \app\admin\model\classroom\Type
     */
    protected $model = null;
    protected $typeList = [];
    protected $multiFields = 'ismenu,status';

    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = ['json'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\classroom\Type;
        $this->view->assign("statusList", $this->model->getStatusList());
        
        // 必须将结果集转换为数组
        $list = \think\Db::name("classroom_type")->order('weigh DESC,id ASC')->select();
        
        Tree::instance()->init($list)->icon = ['&nbsp;&nbsp;&nbsp;&nbsp;', '&nbsp;&nbsp;&nbsp;&nbsp;', '&nbsp;&nbsp;&nbsp;&nbsp;'];
        $this->typeList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
        $typedata = [0 => __('None')];
        foreach ($this->typeList as $k => &$v) {
            $typedata[$v['id']] = $v['name'];
        }
        unset($v);
        
        $this->view->assign('typedata', $typedata);
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * json数据
     */
    public function json()
    {
        $pid = input('pid','0');
        $where = '1=1';
        $where .= " AND pid = {$pid} ";
        $result = collection($this->model->where($where)->order(['weigh'=>'desc','id'=>'desc'])->select())->toArray();

        if($pid > 0 && count($result) == 0){
            array_unshift($result,['id'=>0,'name'=>'无']);
        }
        return json($result);
    }

    
    
    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $list = $this->typeList;
            $total = count($this->typeList);
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a", [], 'strip_tags');
            if ($params) {

                // 判定只有二级分类
                $pid = Db::name("classroom_type")->where('id',$params['pid'])->value('pid');
                if($pid > 0) {
                    $this->error('项目只支持二级栏目');
                }

                $result = $this->model->save($params);
                if ($result === false) {
                    $this->error($this->model->getError());
                }
                $this->success();
            }
            $this->error();
        }
        return $this->view->fetch();
    }
    
    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        if ($ids) {
            $delIds = [];
            foreach (explode(',', $ids) as $k => $v) {
                $delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, true));
            }
            $delIds = array_unique($delIds);
            $count = $this->model->where('id', 'in', $delIds)->delete();
            if ($count) {
                $this->success();
            }
        }
        $this->error();
    }




}